/*
 * name: scroll-load
 * version: 1.0.5
 * updata: bug fix
 * data: 2018-04-17
 */
define('scroll-load', function(require, exports, module) {
    "use strict";
    seajs.importStyle('.scrollLoadSpinning{text-align: center;background:#fff;padding:.3em}\
        .scrollLoadSpinning ._spin{display: block;margin:auto;width:2.5em;height: 2.5em;background-size: contain !important;\
        -webkit-animation: rotation  0.8s infinite linear; animation: rotation  0.8s infinite linear;\
        background:url() center no-repeat;}\
        @-webkit-keyframes rotation { 0% { -webkit-transform: rotateZ(0deg);}\
        100% { -webkit-transform: rotateZ(360deg);}}\
        @keyframes rotation { 0% { transform: rotateZ(0deg);}\
        100% { transform: rotateZ(360deg);}}', module.uri);
    var $ = window.jQuery || require('jquery'),
        base = require('base'),
        def = {
            el: null,
            callback: null,
            force: false,
            distance: 70,
            loadingTemplate: '<div class="scrollLoadSpinning"><span class="_spin"></span></div>'
        },
        scrollLoad = function(config) {
            var opt = $.extend({}, def, config || {}),
                $wrap = $(opt.el).eq(0),
                loadingId,
                scrollDom, 
                viewH, 
                contHeight, 
                scrollCB, 
                running, 
                $loading, 
                destroy;
            if (!$wrap.length) {
                console.warn(opt.el + '不存在');
                return null;
            }
            loadingId = $wrap.data('scroll-load-id') || base.getUUID();
            if ($wrap.is('body')) {
                scrollDom = $(window);
                contHeight = function() {
                    return $(document).height();
                };
            } else {
                scrollDom = $wrap;
                if (!$wrap.find('.scrollLoadCont').length) {
                    $wrap.wrapInner('<div class="scrollLoadCont" />');
                }
                contHeight = function() {
                    return $wrap.children('.scrollLoadCont').height();
                };
            }
            if ($wrap.find('#' + loadingId).length) {
                $loading = $wrap.find('#' + loadingId).css('display','none');
            } else if (opt.loadingTemplate && opt.loadingTemplate.split) {
                $loading = $(opt.loadingTemplate).attr('id', loadingId).css('display','none');
            }
            destroy = function() {
                $wrap.data('scroll-load-id', null);
                $('#' + loadingId).remove();
                scrollDom.unbind('scroll', scrollCB);
                return null;
            };
            if (opt.force) {
                destroy();
            } else {
                if (window.nomore) {
                    return destroy();
                }
                if ($wrap.data('scroll-load-id')) {
                    return null;
                }
            }
            viewH = function() {
                return scrollDom.height();
            };
            scrollCB = function() {
                if (running) {
                    return $wrap;
                }
                var contentH = contHeight(),
                    scrollTop = $(this).scrollTop();
                if (contentH - viewH() - scrollTop < opt.distance) {
                    running = true;
                    //插入加载提示
                    if ($loading.length) {
                        $wrap.append($loading.css('display','block')).scrollTop($wrap.scrollTop() + $loading.outerHeight(true) + opt.distance);
                    } else {
                        $wrap.scrollTop($wrap.height());
                    }

                    if (typeof(opt.callback) === 'function') {
                        opt.callback(function() {
                            $loading && $loading.css('display','none');
                            running = null;
                        });
                    }
                }
            };
            if (!$wrap.data('scroll-load-id')) {
                $wrap.data('scroll-load-id', loadingId);
                scrollDom.on('scroll', scrollCB);
            }
            return {
                destroy: destroy
            };
        };

    $.fn.scrollLoad = function(config) {
        return scrollLoad($.extend({
            el: this
        }, config || {}));
    };
    module.exports = scrollLoad;
});